package com.ruoyi.system.controller;

import com.ruoyi.system.config.AliPayConfig;
import com.ruoyi.system.domain.dto.AliPayDTO;
import com.ruoyi.system.service.IAliPayService;
import com.ruoyi.system.service.IBillInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * @author xy
 * @version 1.0
 * @description:
 * @date 2024/7/6 13:29
 */
@Api(tags = "支付模块")
@RequestMapping("/pay")
@RestController
public class PayController {
    @Autowired
    private IAliPayService aliPayService;

    @Autowired
    private AliPayConfig aliPayConfig;

    @Autowired
    private IBillInfoService billInfoService;

    @ApiOperation(value = "返回支付二维码链接")
    @PostMapping("/alipay")
    public String scanCodePay(@RequestBody AliPayDTO dto) {
        String host = "http://210.42.42.130:2004";
//        String host = "http://t2cxky.natappfree.cc"; // 开发环境
        // 回调接口
        String notifyUrl = host + "/pay/pcCallback?billInfoId=" + dto.getBillId();
        System.out.println(notifyUrl);
        // 请求阿里云支付
        return aliPayService.keyPayment(dto, "FAST_INSTANT_TRADE_PAY", notifyUrl);
    }

    @PutMapping("/callback")
    @ApiOperation(value = "小程序支付成功回调接口")
    public int callBack(@RequestBody AliPayDTO dto) {
        return billInfoService.updateBillInfo(dto.getBillId());
    }

    @PostMapping("/pcCallback")
    @ApiOperation(value = "网页支付成功回调接口")
    public int pcCallBack(@RequestParam("billInfoId") Long billInfoId) {
        return billInfoService.updateBillInfo(billInfoId);
    }
}
